#include <nice2d/node.h>
#include <nice2d/actions/repeataction.h>

class RepeatActionData
{
public:
    std::function<void(NodePointer)> act;
    float interval = 3.0f;
    float elapsedTime = 0.0f;
};

RepeatAction::RepeatAction(std::function<void (NodePointer)> act,
                           float interval):
    data(new RepeatActionData())
{
    assert(act);
    data->act = act;
    data->interval = interval;
}

RepeatAction::~RepeatAction()
{
}

float RepeatAction::getInterval() const
{
    return data->interval;
}

void RepeatAction::update(float deletaTime)
{
    data->elapsedTime += deletaTime;
    if (data->elapsedTime >= getInterval()) {
        auto target = getOwner();
        if (data->act && !target.expired())
            data->act(std::dynamic_pointer_cast<Node>(target.lock()));
        data->elapsedTime = 0.0f;
    }
}